<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Vuex使用State</title>
  <script src="../../node_modules/vue/dist/vue.js"></script>
  <script src="../../node_modules/vuex/dist/vuex.js"></script>
</head>
<body>
  <div id="app">
    <h2>{{msg}}</h2>
    <div @click='increment'>加</div>
    <modal></modal>
  </div>
</body>
<script>
  // import { INCREMENT } from './mutations/mutations-type.js';
  // import { mapMutations} from 'vuex';
  const modal = {
    template: '<div><div>{{ count }}</div><div>{{ name }}</div><div>{{ sayHi }}</div></div>',
    computed:{
      count () {
        return this.$store.state.count
      },
      name(){
        return this.$store.state.name
      },
      sayHi () {
        return this.$store.getters.sayHi
      }
    },
  }
  
  // 创建Store
  let store = new Vuex.Store({
    state:{
      count: 10,
      name: 'jack'
    },
    getters:{
      sayHi (state){
        return state.name + ',hello!!!'
      }
    },
    mutations:{
      increment  (state,num){
        state.count = num
      },
      updateName (state,name){
        state.name = name
      }
    },
    actions:{
      incrementActions(context,num){
        context.commit('increment',num)
      }
    }
  })
  // 生成vue实例
  new Vue({
    el:'#app',
    data:{
      msg:'提交State'
    },
    store,
    components:{
      modal
    },
    methods:{
      // ...mapMutations({
      //   add: 'INCREMENT'
      // })
      increment(){
        // this.$store.commit('increment')
        this.$store.commit('updateName','xiaoShuang')
        this.$store.dispatch('incrementActions',10000)
      },
    },
    
  })
</script>
</html>